package com.wangzhe.builder;

import com.wangzhe.bean.Constants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 王哲
 * @ClassName BuilderBase
 * @create 2023--六月--上午9:03
 * @Description 创建基础类
 * @Version V1.0
 */
public class BuilderBase {

    private static Logger logger = LoggerFactory.getLogger(BuilderBase.class);

    public static void build() {

        List headerPackages = new ArrayList<>();
        //枚举
        headerPackages.add("package " + Constants.ENUM_PACKAGE + ";");
        build(headerPackages, "DateUtils", Constants.ENUM_PACKAGE_PATH);

        headerPackages.clear();
        headerPackages.add("package " + Constants.ENUM_PACKAGE + ";");
        build(headerPackages, "PageSize", Constants.ENUM_PACKAGE_PATH);

        headerPackages.clear();
        headerPackages.add("package " + Constants.ENUM_PACKAGE + ";");
        build(headerPackages, "ResponseCodeEnum", Constants.ENUM_PACKAGE_PATH);

        //公共工具类
        headerPackages.clear();
        headerPackages.add("package " + Constants.UTILS_PACKAGE + ";");
        build(headerPackages, "DateUtils", Constants.UTILS_PACKAGE_PATH);


        //mappers类
        headerPackages.clear();
        headerPackages.add("package " + Constants.MAPPER_PACKAGE + ";");
        build(headerPackages, "BaseMapper", Constants.MAPPER_PACKAGE_PATH);

        //example
        headerPackages.clear();
        headerPackages.add("package " + Constants.EXAMPLE_PATH + ";");
        build(headerPackages, "BaseQuery", Constants.PACKAGE_EXAMPLE_PATH);

        headerPackages.clear();
        headerPackages.add("package " + Constants.EXAMPLE_PATH + ";");
        headerPackages.add("import " + Constants.ENUM_PACKAGE + ".PageSize;");
        build(headerPackages, "SimplePage", Constants.PACKAGE_EXAMPLE_PATH);

        //VO
        headerPackages.clear();
        headerPackages.add("package " + Constants.VO_PATH + ";");
        build(headerPackages, "PaginationResultVO", Constants.PACKAGE_VO_PATH);

        headerPackages.clear();
        headerPackages.add("package " + Constants.VO_PATH + ";");
        build(headerPackages, "ResponseVO", Constants.PACKAGE_VO_PATH);

        //exception
        headerPackages.clear();
        headerPackages.add("package " + Constants.PACKAGE_EXCEPTION + ";");
        headerPackages.add("import " + Constants.ENUM_PACKAGE + ".ResponseCodeEnum;");
        build(headerPackages, "BusinessException", Constants.PACKAGE_EXCEPTION_PATH);

        //controller
        headerPackages.clear();
        headerPackages.add("package " + Constants.PACKAGE_CONTROLLER + ";");
        headerPackages.add("import " + Constants.ENUM_PACKAGE + ".ResponseCodeEnum;");
        headerPackages.add("import " + Constants.VO_PATH + ".ResponseVO;");
        build(headerPackages, "BaseController", Constants.PACKAGE_CONTROLLER_PATH);

        headerPackages.clear();
        headerPackages.add("package " + Constants.PACKAGE_CONTROLLER + ";");
        headerPackages.add("import " + Constants.ENUM_PACKAGE + ".ResponseCodeEnum;");
        headerPackages.add("import " + Constants.VO_PATH + ".ResponseVO;");
        headerPackages.add("import " + Constants.PACKAGE_EXCEPTION + ".BusinessException;");
        build(headerPackages, "ExceptionHandlerController", Constants.PACKAGE_CONTROLLER_PATH);

    }

    public static void build(List headerPackages, String fileName, String outPath) {
        File folder = new File(outPath);
        if (!folder.exists()) {
            folder.mkdirs();
        }

        File file = new File(folder, fileName + ".java");

        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException e) {
                logger.info("创建文件失败:{}", e.getMessage());
            }
        }

        OutputStream os = null;
        OutputStreamWriter osw = null;
        BufferedWriter bw = null;

        InputStream is = null;
        InputStreamReader isr = null;
        BufferedReader br = null;

        try {

            os = new FileOutputStream(file);
            osw = new OutputStreamWriter(os, "UTF-8");
            bw = new BufferedWriter(osw);

            is = BuilderBase.class.getClassLoader().getResourceAsStream("template/" + fileName + ".txt");
            isr = new InputStreamReader(is, "UTF-8");
            br = new BufferedReader(isr);

            for (Object headerPackage : headerPackages) {
                String str = headerPackage.toString();
                bw.write(str);

                if (str.startsWith("package")){
                    bw.newLine();
                    bw.newLine();
                }else if(str.startsWith("import")){
                    bw.newLine();
                }
            }

            String line = null;
            while ((line = br.readLine()) != null) {
                bw.write(line);
                bw.newLine();
                bw.flush();
            }

        } catch (Exception e) {
            logger.info("生成基础类:{}==失败:{}", fileName, e.getMessage());
        } finally {
            try {
                if (br != null) {
                    br.close();
                }
                if (isr != null) {
                    isr.close();
                }
                if (is != null) {
                    is.close();
                }
                if (bw != null) {
                    bw.close();
                }
                if (osw != null) {
                    osw.close();
                }
                if (os != null) {
                    os.close();
                }
            } catch (IOException e) {
                logger.info("关闭流失败:{}", e.getMessage());
            }
        }

    }

}
